package com.heima.behavior.service.impl;

import com.heima.behavior.common.BehaviorConstants;
import com.heima.behavior.service.ApUserRelationService;
import com.heima.behavior.thread.AppThreadLocalUtil;
import com.heima.common.cache.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {
	@Autowired
	private CacheService cacheService;
	@Override
	public ResponseResult follow(UserRelationDto dto) {
		//参数判断
		if (dto == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
		}
		
		//判断登录状态
		ApUser user = AppThreadLocalUtil.getUser();
		Integer userId = user.getId();
		if (user == null || userId == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
		}
		
		//参数获取
		Short operation = dto.getOperation();
		Long articleId = dto.getArticleId();
		Integer authorId = dto.getAuthorId();
		
		//1 关注
		//0 取消关注
		if (operation == 1) {
			cacheService.zAdd(BehaviorConstants.APUSER_FOLLOW_RELATION + userId, authorId.toString(), System.currentTimeMillis());
			cacheService.zAdd(BehaviorConstants.APUSER_FANS_RELATION + authorId, userId.toString(), System.currentTimeMillis());
		
		} else if (operation == 0) {
			cacheService.zRemove(BehaviorConstants.APUSER_FOLLOW_RELATION + userId, authorId.toString());
			cacheService.zRemove(BehaviorConstants.APUSER_FANS_RELATION + userId, userId.toString());
			
		}
		
		return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
	}
}
